﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mevsukovGames
{
    class GameField
    {
        public int Height { get; protected set; }
        public int Width { get; protected set; }
        private bool[,] GField;

        public GameField(int xSize, int ySize)
        {
            Height = ySize;
            Width = xSize;
            GField = new bool[Height, Width];
        }

        public bool AddShape(Shape shp)
        {
            try
            {
                this.GField[shp.FirstBlock.BlockCoordinates.YCoordinate, shp.FirstBlock.BlockCoordinates.XCoordinate] = true;
                this.GField[shp.SecondBlock.BlockCoordinates.YCoordinate, shp.SecondBlock.BlockCoordinates.XCoordinate] = true;
                this.GField[shp.ThirdBlock.BlockCoordinates.YCoordinate, shp.ThirdBlock.BlockCoordinates.XCoordinate] = true;
                this.GField[shp.FourthBlock.BlockCoordinates.YCoordinate, shp.FourthBlock.BlockCoordinates.XCoordinate] = true;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool IsPositionFree(int x, int y)
        {
            try
            {
                if (x >= Width || x < 0)
                {
                    return false;
                }
                else if (!GField[y, x])
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }

        public bool IsPositionFree(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3)
        {
            try
            {
                if (x >= Width || x < 0 || x1 >= Width || x1 < 0 || x2 >= Width || x2 < 0 || x3 >= Width || x3 < 0)
                {
                    return false;
                }
                else if (!GField[y, x] && !GField[y1, x1] && !GField[y2, x2] && !GField[y3, x3])
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }

        public bool IsFieldContains(int x, int y)
        { 
            if (x>=Width || y>=Height || x<0 || y<0)
                return false;
            return true;
        }

        public int ClearLines()
        {
            int clearedLinesCount = 0;
            int filledBlockCount = 0;
            int i = Height - 1;
            for (; i >= 0; i--)
            {
                for (int j = 0; j < Width; j++)
                {
                    if (GField[i, j])
                        filledBlockCount++;
                }
                if (filledBlockCount == Width)
                {
                    clearedLinesCount++;
                    ClearLine(i);
                    i++;
                }
                filledBlockCount = 0;
            }
            return clearedLinesCount;
        }

        public void ClearLine(int lineNumber)
        {
            for (int i = lineNumber; i >= 0; i--)
            {
                for (int j = 0; j < Width; j++)
                {
                    if (i == 0)
                        GField[i, j] = false;
                    else
                        GField[i, j] = GField[i - 1, j];
                }
            }
        }

        
    }
}
